﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;
using Castle.MicroKernel.Registration;
using Castle.Windsor.Installer;

namespace WindsorInitConfig
{
    [TestFixture]
    public class ResolveAllConstructors
    {
        private const string xmlConfig = @"<castle>
     <components>
        <component id='MainClass'
             service='WindsorInitConfig.ResolveAllConstructors+IMyWindsorInterface, WindsorInitConfig'
             type='WindsorInitConfig.ResolveAllConstructors+MyWindsorClass, WindsorInitConfig'
             inspectionBehavior='None'
             lifestyle='Transient'>
       <parameters>
          <obj>${blah}</obj>
       </parameters>
        </component>
    <component id='MainClass2'
               service='WindsorInitConfig.ResolveAllConstructors+IMyWindsorInterface, WindsorInitConfig'
               type='WindsorInitConfig.ResolveAllConstructors+MyWindsorClass, WindsorInitConfig'
               inspectionBehavior='None'
               lifestyle='Transient' />
         <component id='blah' 
             service='WindsorInitConfig.ResolveAllConstructors+IMyInterface, WindsorInitConfig' 
             type='WindsorInitConfig.ResolveAllConstructors+MyInjectedClass, WindsorInitConfig' 
             inspectionBehavior='None'/>
     </components>
</castle>";

        public interface IMyWindsorInterface {
            IMyInterface Dep { get; }
        }

        public class MyWindsorClass : IMyWindsorInterface {
            public IMyInterface Dep { get; private set; }
            public MyWindsorClass() { }
            public MyWindsorClass(IMyInterface obj) {
                this.Dep = obj;
            }
        }

        public interface IMyInterface { }
        public class MyInjectedClass : IMyInterface { }

        [Test]
        public void TestXml() {
            var container = new WindsorContainer();
            container.Kernel.ComponentModelCreated += model => {
                if (model.Name == "MainClass2")
                    model.Constructors.Remove(model.Constructors.First(c => c.Dependencies.Any(m => m.TargetType == typeof(IMyInterface))));
            };
            container.Install(Configuration.FromXml(new StaticContentResource(xmlConfig)));
            var objs = container.ResolveAll<IMyWindsorInterface>();
            Assert.AreEqual(1, objs.Count(o => o.Dep == null));
            Assert.AreEqual(1, objs.Count(o => o.Dep != null));
        }

        [Test]
        public void TestNoXml()
        {
            //var container = new WindsorContainer();
            //container.Register(Component.For<IMyInterface>().ImplementedBy<MyInjectedClass>());
            //container.Register(Component.For<IMyWindsorInterface>().ImplementedBy<MyWindsorClass>().Named("MainClass").ServiceOverrides(ServiceOverride.ForKey("obj").Eq(""));
            //container.Register(Component.For<IMyWindsorInterface>().ImplementedBy<MyWindsorClass>().Named("MainClass2"));
        }

    }
}
